/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.ContextMenu;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 *
 * @author kylin
 */
public class TestJavafxContextMenu extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {
        ContextMenu contextMenu=new ContextMenu();
        
        contextMenu.setOnShowing(new EventHandler<WindowEvent>(){
            @Override
            public void handle(WindowEvent event) {
                System.out.println("showing");
            }
        });
        
        contextMenu.setOnShown(new EventHandler<WindowEvent>(){
            @Override
            public void handle(WindowEvent event) {
                System.out.println("shown");
            }
        
        });
        
        MenuItem itm1=new MenuItem("about");
        MenuItem itm2=new MenuItem("preference");
        
        contextMenu.getItems().addAll(itm1,itm2);
        
        TextField txt=new TextField("type to show");
        txt.setContextMenu(contextMenu);
        
//        txt.setOnAction(new EventHandler<ActionEvent>(){
//            @Override
//            public void handle(ActionEvent event) {
//                contextMenu.show(txt,Side.BOTTOM,0,0);
//            }
//        });
        txt.setOnKeyTyped(new EventHandler<KeyEvent>(){
            @Override
            public void handle(KeyEvent event) {
                MenuItem newitm=new MenuItem(txt.getText());
                contextMenu.getItems().add(newitm);
                contextMenu.show(txt,Side.BOTTOM,0,0);
            }
        });
        
        ImageView img=new ImageView(new Image(this.getClass().getResourceAsStream("12681806.jpg")));
        img.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
            @Override
            public void handle(MouseEvent e) {
                System.out.println("clicked!");
                contextMenu.show(img,e.getScreenX(),e.getScreenY());
            }
        });
        
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(5, 5, 5, 5));
        vBox.setSpacing(5);
        vBox.getChildren().addAll(txt);
        StackPane root = new StackPane();
        root.getChildren().addAll(vBox);
        Scene scene = new Scene(root, 350, 250);
 
        primaryStage.setTitle("contextmenu");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
